//===----------------------------------------------------------------------===//
//
//                         BusTub
//
// index_scan_executor.cpp
//
// Identification: src/execution/index_scan_executor.cpp
//
// Copyright (c) 2015-19, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "execution/executors/index_scan_executor.h"

namespace bustub {
IndexScanExecutor::IndexScanExecutor(ExecutorContext *exec_ctx, const IndexScanPlanNode *plan)
    : AbstractExecutor(exec_ctx), plan_(plan) {}

void IndexScanExecutor::Init() {
  Catalog *catalog = exec_ctx_->GetCatalog();
  index_info_ = catalog->GetIndex(plan_->index_oid_);
  auto tree = dynamic_cast<BPlusTreeIndexForTwoIntegerColumn *>(index_info_->index_.get());
  iter = tree->GetBeginIterator();
}

auto IndexScanExecutor::Next(Tuple *tuple, RID *rid) -> bool {
  Catalog *catalog = exec_ctx_->GetCatalog();
  TableInfo *table_info_ = catalog->GetTable(index_info_->table_name_);
  while (not iter.IsEnd()) {
    RID cur_rid = (*iter).second;
    auto [meta, cur_tuple] = table_info_->table_->GetTuple(cur_rid);
    if (not meta.is_deleted_) {
      *tuple = std::move(cur_tuple);
      *rid = cur_rid;
      ++iter;
      return true;
    }
    ++iter;
  }
  return false;
}

}  // namespace bustub
